package com.cloud.wms.ui.returned.presenter;

import android.text.TextUtils;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.entity.ContainerEntity;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.returned.contact.InventoryContract;
import com.cloud.wms.ui.returned.entity.InventoryDetailEntity;
import com.cloud.wms.ui.returned.entity.InventoryInfoEntity;

import java.util.ArrayList;
import java.util.HashMap;

import javax.inject.Inject;

/**
 * @Author： Chen
 * @Date： 2021/5/19
 * @Desc：
 */
public class InventoryPresenter extends BaseRxPresenter<InventoryContract.View> implements InventoryContract.Presenter {
    public static final int GET_RETURNED_COMMODITY = 0x100;
    public static final int CHECK_RETURNED_BOX = 0x101;
    public static final int CHECK_RETURNED_COMMODITY = 0x102;
    public static final int CONFIRM_RETURN_INVENTORY = 0x103;
    public static final int GET_INVENTORY_COMMODITY = 0x104;
    public static final int DELETE_INVENTORY_COMMODITY = 0x105;

    @Inject
    public InventoryPresenter() {
    }

    @Override
    public void getReturnedInfo(String carryCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("ExpressDeliveryCode", carryCode);
        FineExApplication.component().http()
                .getReturnedInfo(params)
                .map(new ResponseWrapper<InventoryInfoEntity>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<InventoryInfoEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(InventoryInfoEntity infoEntity) {
                        mView.onSuccess(MessageCreator.createMessage(infoEntity, GET_RETURNED_COMMODITY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    @Override
    public void queryCommodity(String barCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("BarCode", barCode);
        FineExApplication.component().http()
                .checkReturnedCommodity(params)
                .map(new ResponseWrapper<InventoryInfoEntity.CommodityListBean>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<InventoryInfoEntity.CommodityListBean>(mView.provideContext()) {
                    @Override
                    public void onSuccess(InventoryInfoEntity.CommodityListBean commodityBean) {
                        mView.onSuccess(MessageCreator.createMessage(commodityBean, CHECK_RETURNED_COMMODITY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    @Override
    public void checkBoxCode(String boxCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("ContainerCode", boxCode);
        FineExApplication.component().http()
                .checkReturnedBox(params)
                .map(new ResponseWrapper<ContainerEntity>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ContainerEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ContainerEntity containerEntity) {
                        mView.onSuccess(MessageCreator.createMessage(containerEntity.getContainerCode(), CHECK_RETURNED_BOX));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    @Override
    public void getReturnedInventoryDetail(String carryCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("ExpressDeliveryCode", carryCode);
        FineExApplication.component().http()
                .getReturnedInventoryDetail(params)
                .map(new ResponseWrapper<ArrayList<InventoryDetailEntity>>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<ArrayList<InventoryDetailEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(ArrayList<InventoryDetailEntity> infos) {
                        if (infos == null || infos.isEmpty()) {
                            mView.showEmpty();
                        } else {
                            mView.showContent();
                            mView.onSuccess(MessageCreator.createMessage(infos, GET_INVENTORY_COMMODITY));
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                        mView.showError();
                    }
                });
    }

    @Override
    public void deleteInventoryCommodity(String carryCode, InventoryDetailEntity detailEntity) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("ExpressDeliveryCode", carryCode);
        params.put("ContainerCode", detailEntity.getBoxCode());
        params.put("commodityID", detailEntity.getCommodityId());
        params.put("stockType", detailEntity.getStockType());
        params.put("productBatchID", detailEntity.getProductBatchID());
        params.put("amount", detailEntity.getAmount());

        FineExApplication.component().http()
                .deleteInventoryCommodity(params)
                .map(new ResponseWrapper<Object>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object info) {
                        mView.onSuccess(MessageCreator.createMessage(info, DELETE_INVENTORY_COMMODITY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    @Override
    public void submitReturnedInventory(String carryCode,
                                        String containerCode,
                                        InventoryInfoEntity.CommodityListBean commodityBean,
                                        InventoryInfoEntity.CommodityListBean.BatchInfoBean batchInfoBean,
                                        boolean isNext) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("ExpressDeliveryCode", carryCode);
        params.put("ContainerCode", containerCode);
        params.put("memberID", commodityBean.getMemberID());

        ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("commodityID", commodityBean.getCommodityId());
        map.put("stockType", commodityBean.getStockType());
        if (!TextUtils.isEmpty(commodityBean.getLogisticsCode())) {
            map.put("logisticsCode", commodityBean.getLogisticsCode());
        }
        if (!TextUtils.isEmpty(batchInfoBean.getProductionDate())) {
            map.put("productionDate", batchInfoBean.getProductionDate());
        }
        if (!TextUtils.isEmpty(batchInfoBean.getExpirationDate())) {
            map.put("expirationDate", batchInfoBean.getExpirationDate());
        }
        if (!TextUtils.isEmpty(batchInfoBean.getInDate())) {
            map.put("inDate", batchInfoBean.getInDate());
        }
        map.put("productBatchID", batchInfoBean.getProductBatchID());
        map.put("amount", 1);
        hashMaps.add(map);

        params.put("commodityList", hashMaps);

        FineExApplication.component().http()
                .submitReturnedInventory(params)
                .map(new ResponseWrapper<Object>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object info) {
                        mView.onSuccess(MessageCreator.createMessage(isNext, CONFIRM_RETURN_INVENTORY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
